package com.project.controller.web;

import com.project.entity.Department;
import com.project.myBeans.R;
import com.project.service.DepartmentService;
import com.project.service.SchoolService;
import com.project.service.TeacherInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@EnableAutoConfiguration
@RestController
@RequestMapping(value = "/department")
@Api(description = "系接口",value = "department")
public class DepartmentController {

    @Autowired
    DepartmentService departmentService;

    @Autowired
    SchoolService schoolService;

    @Autowired
    TeacherInfoService teacherInfoService;
    /**
     * wwh 2018/3/22
     * @param department
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加系")
    public R add(@RequestBody Department department){
        try{
            Department isExist = new Department();
            isExist.setName(department.getName());
            isExist.setSchoolid(department.getSchoolid());
            if(departmentService.findByCondition(isExist).size()!=0){
                return R.error("此系已存在");
            }else {
                department.setUniversityid(schoolService.findById(department.getSchoolid()).getUniversityid());
                departmentService.add(department);
                return R.ok();
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * wwh 2018/3/22
     * @param department
     * @return
     */
    @PostMapping("/findByCondition")
    @ApiOperation("按条件查询/模糊查询")
    public R findByCondtion(@RequestBody Department department){
        try{
            List<Department> departmentList = departmentService.findByCondition(department);
            return R.ok().put("departmentList",departmentList);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * Tate 2018/5/23
     *
     * @param teacherid
     * @return
     */
    @ApiOperation("根据教师id查找该学院的所有系部")
    @PostMapping("/finddepartmentbyteacherid/{teacherid}")
    public R finddepartmentbyteacherid(@PathVariable Integer teacherid) {
        try {
            Integer schoolid = teacherInfoService.findById(teacherid).getSchoolid();
            List<Department> list = departmentService.findBySchoolid(schoolid);
            return R.ok().put("data", list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * Tate 2018/5/23
     *
     * @param schoolid
     * @return
     */
    @ApiOperation("根据学院id查找该学院的所有系部")
    @GetMapping("/finddepartmentbyschoolid/{schoolid}")
    public R finddepartmentbyschoolid(@PathVariable Integer schoolid) {
        try {
            return R.ok("获取成功").put("data", departmentService.findBySchoolid(schoolid));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }

}
